package com.atguigu.eduservice.controller;

import com.atguigu.commonutils.result.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exception.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author kabuda
 * @create 2023-05-07 11:10
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @PostMapping
    public R save(@RequestBody EduVideo eduVideo) {
        boolean save = eduVideoService.save(eduVideo);
        if (save) {
            return R.ok();
        }
        throw new MyException(20001, "新增失败");
    }

    @PutMapping
    public R update(@RequestBody EduVideo eduVideo) {
        boolean b = eduVideoService.updateById(eduVideo);
        if (b) {
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("{videoId}")
    public R delete(@PathVariable("videoId") String videoId) {
        eduVideoService.removeVideo(videoId);
        return R.ok();
    }

    @GetMapping("{id}")
    public R getVideo(@PathVariable String id) {
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("eduVideo", eduVideo);
    }
}
